﻿Imports System.Data.SqlClient
Imports System.Management
Public Class clsUsuarioPrincipal
    'Usuarios
    Private mintIdUsuario As Integer
    Private mstrNombre As String
    Private mstrNombreCompleto As String
    Private mstrPassword As String
    'Empresas
    Private mintIdEmpresa As Integer
    Private mstrNombreEmpresa As String
    Private mstrBD As String
    Private mstrContabilidad As String
    Private mstrNominas As String
    Private mstrAdminPAQ As String
    Private mstrRespaldo As String

    Public Property IdUsuario() As Integer
        Get
            Return mintIdUsuario
        End Get
        Set(ByVal value As Integer)
            mintIdUsuario = value
        End Set
    End Property

    Public Property Nombre() As String
        Get
            Return mstrNombre
        End Get
        Set(ByVal value As String)
            mstrNombre = value
        End Set
    End Property
    Public Property NombreCompleto() As String
        Get
            Return mstrNombreCompleto
        End Get
        Set(ByVal value As String)
            mstrNombreCompleto = value
        End Set
    End Property
    Public Property Password() As String
        Get
            Return mstrPassword
        End Get
        Set(ByVal value As String)
            mstrPassword = value
        End Set
    End Property
    Public Property IdEmpresa() As Integer
        Get
            Return mintIdEmpresa
        End Get
        Set(ByVal value As Integer)
            mintIdEmpresa = value
        End Set
    End Property
    Public Property NombreEmpresa() As String
        Get
            Return mstrNombreEmpresa
        End Get
        Set(ByVal value As String)
            mstrNombreEmpresa = value
        End Set
    End Property
    Public Property BD() As String
        Get
            Return mstrBD
        End Get
        Set(ByVal value As String)
            mstrBD = value
        End Set
    End Property
    Public Property Contabilidad() As String
        Get
            Return mstrContabilidad
        End Get
        Set(ByVal value As String)
            mstrContabilidad = value
        End Set
    End Property
    Public Property Nominas() As String
        Get
            Return mstrNominas
        End Get
        Set(ByVal value As String)
            mstrNominas = value
        End Set
    End Property
    Public Property AdminPAQ() As String
        Get
            Return mstrAdminPAQ
        End Get
        Set(ByVal value As String)
            mstrAdminPAQ = value
        End Set
    End Property
    Public Property Respaldo() As String
        Get
            Return mstrRespaldo
        End Get
        Set(ByVal value As String)
            mstrRespaldo = value
        End Set
    End Property
    Public Sub New()
        mintIdUsuario = 0
        mstrNombre = ""
        mstrNombreCompleto = ""
        mstrPassword = ""
        mintIdEmpresa = 0
        mstrNombreEmpresa = ""
        mstrBD = ""
        mstrContabilidad = ""
        mstrNominas = ""
        mstrAdminPAQ = ""
        mstrRespaldo = ""
    End Sub
    Public Sub InsertarUsuario()
        Dim sql As String = "Insert Into Usuarios (IdUsuario,Nombre,NombreCompleto,Password) Values (@IdUsuario,@Nombre,@NombreCompleto,@Password)"
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = InitialConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IdUsuario", mintIdUsuario)
        Command.Parameters.AddWithValue("Nombre", mstrNombre)
        Command.Parameters.AddWithValue("NombreCompleto", mstrNombreCompleto)
        Command.Parameters.AddWithValue("Password", mstrPassword)

        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
            MsgBox("Usuario Principal Creado con exito", MsgBoxStyle.Information, "Aviso")

        Else
            MsgBox("No fue posible insertar el usuario", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If

    End Sub
    Public Sub InsertarEmpresa()
        Dim sql As String = "Insert Into Empresas (IdEmpresa,Nombre,BD,Contabilidad,Nominas,AdminPAQ,Respaldo) Values (@IdEmpresa,@Nombre,@BD,@Contabilidad,@Nominas,@AdminPAQ,@Respaldo)"
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = InitialConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("IdEmpresa", mintIdEmpresa)
        Command.Parameters.AddWithValue("Nombre", mstrNombreEmpresa)
        Command.Parameters.AddWithValue("BD", mstrBD)
        Command.Parameters.AddWithValue("Contabilidad", mstrContabilidad)
        Command.Parameters.AddWithValue("Nominas", mstrNominas)
        Command.Parameters.AddWithValue("AdminPAQ", mstrAdminPAQ)
        Command.Parameters.AddWithValue("Respaldo", mstrRespaldo)

        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
            MsgBox("Empresa Creada con Exito", MsgBoxStyle.Information, "Aviso")

        Else
            MsgBox("No fue posible insertar el usuario", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If

    End Sub
    Function consecutivo() As Double
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = InitialConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = "SELECT Max(IdUsuario) As id From USUARIOS "
        Dim rdBuscar As SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        If rdBuscar.Read() Then
            consecutivo = rdBuscar("ID") + 1
        Else
            consecutivo = 1
        End If

        rdBuscar.Close()
        cnConn.Close()
    End Function
    Function consecutivoEmpresas() As Double
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = InitialConnection

        Dim cmdBuscar As New SqlCommand
        cmdBuscar.Connection = cnConn
        cmdBuscar.CommandText = "SELECT Max(IdEmpresa) As id From Empresas "
        Dim rdBuscar As SqlDataReader

        cnConn.Open()
        rdBuscar = cmdBuscar.ExecuteReader

        If rdBuscar.Read() Then
            consecutivoEmpresas = rdBuscar("ID") + 1
        Else
            consecutivoEmpresas = 1
        End If

        rdBuscar.Close()
        cnConn.Close()
    End Function
    Public Sub InsertaUsuarioConectado()
        Dim sql As String = "Insert Into UsuariosConectados (Usuario,Ubicacion,Equipo) Values (@Usuario,@Ubicacion,@Equipo)"
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = InitialConnection

        Dim Command As New SqlCommand(sql, cnConn)
        Dim Fecha As DateTime = Now()
        Dim IP As Net.Dns
        Dim PC As String
        Dim Hora As String = Now.ToShortTimeString

        PC = IP.GetHostName
        
        Command.Parameters.AddWithValue("Usuario", UsuarioActivo)
        Command.Parameters.AddWithValue("Ubicacion", "Empresas")
        Command.Parameters.AddWithValue("Equipo", PC)

        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el usuario", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Public Sub EliminaUsuarioConectado()
        Dim Fecha As DateTime = Now()
        Dim IP As Net.Dns
        Dim PC As String
        Dim Hora As String = Now.ToShortTimeString
        PC = IP.GetHostName

       
        Dim sql As String = "DELETE FROM UsuariosConectados WHERE Usuario='" & UsuarioActivo & "' AND Equipo='" & PC & "'"
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = InitialConnection
        Dim Command As New SqlCommand(sql, cnConn)
        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el usuario", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Sub Ubicacion(ByVal Actual As String)
       
        Dim Fecha As DateTime = Now()
        Dim IP As Net.Dns
        Dim PC As String
        Dim Hora As String = Now.ToShortTimeString

        PC = IP.GetHostName

        Dim sql As String = "UPDATE UsuariosConectados Set Ubicacion='" & Actual & "' WHERE Usuario='" & UsuarioActivo & "' AND Equipo='" & PC & "'"
        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = InitialConnection

        Dim Command As New SqlCommand(sql, cnConn)

        cnConn.Open()
        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar la ubicación", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
End Class
